<template>
  <el-dialog
    title="岗位设置"
    :close-on-click-modal="false"
    :visible.sync="flagVisibleSet"
    width="50%"
    :before-close="handleClose"
  >
    <el-main>
      <el-checkbox
        :indeterminate="isIndeterminate"
        v-model="checkAll"
        @change="handleCheckAllChange"
        >全选</el-checkbox
      >
      <div class="marginT"></div>
      <el-checkbox-group
        v-model="checkedPositions"
        @change="handleCheckedPositionChange"
      >
        <el-checkbox
          v-for="position in positions"
          :label="position"
          :key="position"
          >{{ position }}</el-checkbox
        >
      </el-checkbox-group>
    </el-main>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button
        type="primary"
        :disabled="saveDisabled"
        @click="confirmPosition(checkedPositions)"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import success from "@/utils/operation-message.js"
import {
  getStaffDutyByStaffId,
  addStaffDutyBatch,
  getDutyAll
} from "@/api/setup/doctors-setup.js"
export default {
  components: {},
  props: {
    flagVisibleSet: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      staffRow: null,
      isIndeterminate: true, // 全选
      saveDisabled: false,
      checkAll: false,
      dutyInfos: [],
      positions: [], // 所有项
      ptions: [],
      checkedPositions: [], // 单选框选中
      originPositions: []
      // positions: ["发热门诊", "收费室", "门诊医生", "住院医生", "科主任"]
    }
  },
  methods: {
    // 获取复选框数据
    init(row) {
      this.checkedPositions = [] // 选中项
      this.staffRow = row // 信息
      this.setPositionsChecked()
    },
    async setPositionsChecked() {
      await this.getStaffDutyAll()
      this.getMedicalStaffDuty()
    },
    // 查询所有的项
    async getStaffDutyAll() {
      const res = await getDutyAll()
      if (res.code === "1000") {
        this.dutyInfos = res.data
        this.positions = res.data.map(e => {
          return e.name
        })
      }
      // console.log(this.positions)
    },
    async getMedicalStaffDuty() {
      const res = await getStaffDutyByStaffId(this.staffRow.hisMedicalStaffId)
      // console.log(res)
      if (res.code === "1000") {
        this.ptions = res.data
        for (let i = 0; i < this.ptions.length; i++) {
          for (let j = 0; j < this.dutyInfos.length; j++) {
            if (this.ptions[i].dutyId == this.dutyInfos[j].hisDutyInfoId) {
              this.checkedPositions.push(this.dutyInfos[j].name)
              break
            }
          }
        }
        // 之前的岗位
        this.originPositions = this.checkedPositions.concat()
        // console.log(this.originPositions)
        this.$emit("update:flagVisibleSet", true)
      }
    },
    // 关闭弹框
    handleClose() {
      this.$emit("update:flagVisibleSet", false)
    },
    // 取消按钮
    cancel() {
      this.handleClose()
    },
    // 确认提交
    async confirmPosition(checkedPositions) {
      // 将原有的岗位的数组与最终的岗位数组组合成一个数组并去重
      let combArr = [...new Set(this.originPositions.concat(checkedPositions))]
      // console.log(combArr)
      // let insertDutyIdArr = []
      // let deleteDutyIdArr = []
      // for (let i = 0; i < combArr.length; i++) {
      //   if (!this.originPositions.includes(combArr[i])) {
      //     insertDutyIdArr.push(this.transDutyIdByName(combArr[i]))
      //   }
      // }
      // console.log(insertDutyIdArr)
      // for (let j = 0; j < combArr.length; j++) {
      //   if (!checkedPositions.includes(combArr[j])) {
      //     deleteDutyIdArr.push(this.transDutyIdByName(combArr[j]))
      //   }
      // }
      // console.log(deleteDutyIdArr)
      // let deleteStaffDutyArr = this.getDeleteStaffDuty(deleteDutyIdArr)
      // console.log(deleteStaffDutyArr)

      let dutyIds = []
      for (let i = 0; i < checkedPositions.length; i++) {
        dutyIds.push(this.transDutyIdByName(checkedPositions[i]))
      }
      console.log(dutyIds)

      let hisMedicalStaffDutySet = {
        hisMedicalStaffId: this.staffRow.hisMedicalStaffId,
        dutyIds: dutyIds.toString()
      }
      console.log(hisMedicalStaffDutySet)
      this.saveDisabled = true
      const res = await addStaffDutyBatch(hisMedicalStaffDutySet)
      if (res.code === "1000") {
        this.saveDisabled = false
        this.handleClose()
        success()
      } else {
        this.$message({
          message: res.msg,
          type: "error",
          duration: 2000
        })
        this.saveDisabled = false
      }
    },
    transDutyIdByName(name) {
      for (let j = 0; j < this.dutyInfos.length; j++) {
        if (name == this.dutyInfos[j].name) {
          return this.dutyInfos[j].hisDutyInfoId
        }
      }
    },
    getDeleteStaffDuty(deleteDutyIdArr) {
      let delStaffDutyArr = []
      for (let i = 0; i < deleteDutyIdArr.length; i++) {
        for (let j = 0; j < this.ptions.length; j++) {
          if (this.ptions[j].dutyId == deleteDutyIdArr[i]) {
            delStaffDutyArr.push(this.ptions[j])
            break
          }
        }
      }
      return delStaffDutyArr
    },
    // 全选是否选中
    handleCheckAllChange(val) {
      this.checkedPositions = val ? this.positions : []
      this.isIndeterminate = false
    },
    // 点击单选选中
    handleCheckedPositionChange(value) {
      // console.log(this.positions)
      // console.log(value)
      let checkedCount = value.length
      this.checkAll = checkedCount === this.positions.length
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.positions.length
    }
  }
}
</script>
<style lang="scss" scoped>
.el-dialog {
  .el-main {
    margin-left: 30px;
    .marginT {
      padding: 5px 0;
    }
  }
}
</style>
